package gui.panel_new;


import enums.*;
import j_util.Utiles;
import misc.BundlesProvider;
import model.Polisilogismo;
import model.Premisa;
import model.Sylogism;

import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ResourceBundle;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Destinada al aprendizaje de los polisilogismos;
 * @author Jose Martin Rozanec;
 */
public class PolisilogismoPanel extends OpenSavePanel {

    private javax.swing.JButton ascendenteButton;
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JComboBox combo1;
    private javax.swing.JComboBox combo2;
    private javax.swing.JComboBox combo3;
    private javax.swing.JButton completeButton;
    private javax.swing.JLabel completeLabel;
    private javax.swing.JTextField conclusionField;
    private javax.swing.JLabel conclusionLabel;
    private javax.swing.JLabel convertirEnLabel;
    private javax.swing.JPanel cuadroPanel;
    private javax.swing.JButton descendenteButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JTextField premisaMayorField;
    private javax.swing.JLabel premisaMayorLabel;
    private javax.swing.JTextField premisaMenorField;
    private javax.swing.JLabel premisaMenorLabel;
    private javax.swing.JButton resetButton;
    private javax.swing.JTextArea textArea;
    private javax.swing.JLabel validLabel;
    private javax.swing.JPanel analisisPanel;
    private javax.swing.JTextArea analisisTextArea;
    private JScrollPane jScrollPane2;
    private int state = 0;//-1 if descendente, 1 if ascendente
    private int premisaLabelsStringLength;

    private Sylogism sylogism;
    private Polisilogismo polisilogismo;
    private ResourceBundle textBundle;

    /**
     * Constructor;
     *
     * @param saveButton  - saveButton;
     * @param openButton  - openButton;
     * @param fileChooser - fileChooser;
     */
    public PolisilogismoPanel(JButton saveButton, JButton openButton, JFileChooser fileChooser) {
        this.saveButton = saveButton;
        this.openButton = openButton;
        this.fileChooser = fileChooser;
        this.polisilogismo = new Polisilogismo();
        sylogism = new Sylogism();
        premisaLabelsStringLength = 55;
        textBundle = BundlesProvider.getBundle(URLEnum.TEXT_BUNDLE);
        fileFilter = FileFilterEnum.XML;
        initComponents();
    }

    /**
     * Inicializa los componentes;
     */
    private void initComponents() {
        mainPanel = new javax.swing.JPanel();
        cuadroPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        premisaMayorField = new javax.swing.JTextField();
        premisaMayorLabel = new javax.swing.JLabel();
        convertirEnLabel = new javax.swing.JLabel();
        combo1 = new javax.swing.JComboBox();
        premisaMenorLabel = new javax.swing.JLabel();
        premisaMenorField = new javax.swing.JTextField();
        combo2 = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        conclusionLabel = new javax.swing.JLabel();
        conclusionField = new javax.swing.JTextField();
        combo3 = new javax.swing.JComboBox();
        ascendenteButton = new javax.swing.JButton();
        descendenteButton = new javax.swing.JButton();
        resetButton = new javax.swing.JButton();
        completeLabel = new javax.swing.JLabel();
        validLabel = new javax.swing.JLabel();
        completeButton = new javax.swing.JButton();
        bottomPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        analisisPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        analisisTextArea = new javax.swing.JTextArea();


        mainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.POLISILOGISMO.toString())));

        cuadroPanel.setBackground(new java.awt.Color(255, 255, 255));
        cuadroPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.SILOGISMO.toString())));

        premisaMayorLabel.setText(Utiles.getFormattedString(
                textBundle.getString(TextBundleEnum.PREMISA_MAYOR.toString()), ""));
        premisaMayorLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        premisaMayorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));

        convertirEnLabel.setText(textBundle.getString(TextBundleEnum.CONVERTIR_EN.toString()));

        combo1.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"A", "E", "I", "O"}));
        combo1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                comboConvertOrComplete(PremisaTypeEnum.FIRST, combo1.getSelectedIndex());
            }
        });

        premisaMenorLabel.setText(
                Utiles.getFormattedString(textBundle.getString(TextBundleEnum.PREMISA_MENOR.toString()), ""));
        premisaMenorLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        premisaMenorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));

        combo2.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"A", "E", "I", "O"}));
        combo2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                comboConvertOrComplete(PremisaTypeEnum.FIRST, combo2.getSelectedIndex());
            }
        });

        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));

        conclusionLabel.setText(
                Utiles.getFormattedString(textBundle.getString(TextBundleEnum.CONCLUSION.toString()), ""));
        conclusionLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        conclusionLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.CONCLUSION_LARGO)));

        combo3.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"A", "E", "I", "O"}));
        combo3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                comboConvertOrComplete(PremisaTypeEnum.CONCLUSION, combo3.getSelectedIndex());

            }
        });

        premisaMayorField.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                analize();
                if (Premisa.isComplete(premisaMayorField.getText())) {
                    sylogism.setPremisa(new Premisa(premisaMayorField.getText()), PremisaTypeEnum.FIRST);
                    premisaMayorField.setForeground(new Color(0, 0, 0));
                    premisaMayorLabel.setText(
                            Utiles.getFormattedString(textBundle.getString(
                                    TextBundleEnum.PREMISA_MAYOR.toString()), "  -  " +
                                    textBundle.getString(TextBundleEnum.EXTENSION_PARTES.toString()) + ": " +
                                    sylogism.getPremisa(PremisaTypeEnum.FIRST).getTermsExtensionString()));
                    premisaMayorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_BREVE)));
                } else {
                    sylogism.setPremisa(null, PremisaTypeEnum.FIRST);
                    premisaMayorField.setForeground(new Color(255, 0, 0));
                    premisaMayorLabel.setText(Utiles.getFormattedString(
                            textBundle.getString(TextBundleEnum.PREMISA_MAYOR.toString()), ""));
                    premisaMayorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));
                }
            }
        });

        premisaMenorField.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                analize();
                if (Premisa.isComplete(premisaMenorField.getText())) {
                    sylogism.setPremisa(new Premisa(premisaMenorField.getText()), PremisaTypeEnum.SECOND);
                    premisaMenorField.setForeground(new Color(0, 0, 0));
                    premisaMenorLabel.setText(Utiles.getFormattedString(textBundle.getString(
                            TextBundleEnum.PREMISA_MENOR.toString()), "  -  " +
                            textBundle.getString(TextBundleEnum.EXTENSION_PARTES.toString()) + ": " +
                            sylogism.getPremisa(PremisaTypeEnum.SECOND).getTermsExtensionString()));
                    premisaMenorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_BREVE)));
                } else {
                    sylogism.setPremisa(null, PremisaTypeEnum.SECOND);
                    premisaMenorField.setForeground(new Color(255, 0, 0));
                    premisaMenorLabel.setText(Utiles.getFormattedString(
                            textBundle.getString(TextBundleEnum.PREMISA_MENOR.toString()), ""));
                    premisaMenorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));
                }
            }
        });

        conclusionField.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                analize();
                if (Premisa.isComplete(conclusionField.getText())) {
                    sylogism.setPremisa(new Premisa(conclusionField.getText()), PremisaTypeEnum.CONCLUSION);
                    conclusionField.setForeground(new Color(0, 0, 0));
                    conclusionLabel.setText(Utiles.getFormattedString(textBundle.getString(
                            TextBundleEnum.CONCLUSION.toString()), "  -  " +
                            textBundle.getString(TextBundleEnum.EXTENSION_PARTES.toString()) + ": " +
                            sylogism.getPremisa(PremisaTypeEnum.CONCLUSION).getTermsExtensionString()));
                    conclusionLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.CONCLUSION_BREVE)));
                } else {
                    sylogism.setPremisa(null, PremisaTypeEnum.CONCLUSION);
                    conclusionField.setForeground(new Color(255, 0, 0));
                    conclusionLabel.setText(Utiles.getFormattedString(
                            textBundle.getString(TextBundleEnum.CONCLUSION.toString()), ""));
                    conclusionLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.CONCLUSION_LARGO)));
                }
            }
        });

        ascendenteButton.setText(textBundle.getString(TextBundleEnum.ASCENDENTE.toString()));
        ascendenteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (sylogism.validate()) {
                    if (polisilogismo.initialSylogismWasSet()) {
                        if (state == 1) {
                            polisilogismo.addAscendente(sylogism.getPremisa(PremisaTypeEnum.FIRST),
                                    sylogism.getPremisa(PremisaTypeEnum.SECOND));
                        }
                        if (state == -1) {
                            polisilogismo.addDescendente(sylogism.getPremisa(PremisaTypeEnum.SECOND),
                                    sylogism.getPremisa(PremisaTypeEnum.CONCLUSION));
                        }
                    } else {
                        polisilogismo.setInitialSylogism(sylogism);
                    }
                    state = 1;
                    setAscendenteTextField();
//                    textArea.setText(polisilogismo.toString());//todo ver!
                } else {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                            textBundle.getString(TextBundleEnum.WARNING_SILOGISMO_PROPUESTO_INVALIDO.toString()),
                            "", JOptionPane.WARNING_MESSAGE);
                    if (polisilogismo.initialSylogismWasSet()) {
                        setAscendenteTextField();
                    }
                }
            }
        });
        ascendenteButton.setMnemonic(KeyEvent.VK_A);

        descendenteButton.setText(textBundle.getString(TextBundleEnum.DESCENDENTE.toString()));
        descendenteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (sylogism.validate()) {
                    if (polisilogismo.initialSylogismWasSet()) {
                        if (state == 1) {
                            polisilogismo.addAscendente(sylogism.getPremisa(PremisaTypeEnum.FIRST),
                                    sylogism.getPremisa(PremisaTypeEnum.SECOND));
                        }
                        if (state == -1) {
                            polisilogismo.addDescendente(sylogism.getPremisa(PremisaTypeEnum.SECOND),
                                    sylogism.getPremisa(PremisaTypeEnum.CONCLUSION));
                        }
                    } else {
                        polisilogismo.setInitialSylogism(sylogism);
                    }
                    textArea.setText(polisilogismo.toString());
                    state = -1;
                    setDescendenteTextField();

                } else {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                            textBundle.getString(TextBundleEnum.WARNING_SILOGISMO_PROPUESTO_INVALIDO.toString()),
                            "", JOptionPane.WARNING_MESSAGE);
                    if (polisilogismo.initialSylogismWasSet()) {
                        setDescendenteTextField();
                    }
                }
            }
        });
        descendenteButton.setMnemonic(KeyEvent.VK_D);

        resetButton.setText(textBundle.getString(TextBundleEnum.RESET.toString()));
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetAll();
            }
        });
        resetButton.setMnemonic(KeyEvent.VK_R);

        analisisPanel.setBackground(new java.awt.Color(255, 255, 255));
        analisisPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.ESTADO.toString())));

        completeLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.INCOMPLETO)));

        validLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.INVALIDO)));

        completeButton.setText(textBundle.getString(TextBundleEnum.COMPLETAR.toString()));
        completeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sylogism.complete();
                setSylogism(sylogism);
            }
        });
        completeButton.setMnemonic(KeyEvent.VK_C);

        bottomPanel.setBorder(javax.swing.BorderFactory.createCompoundBorder(
                javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                        new java.awt.Color(204, 204, 204), 1, true),
                        textBundle.getString(TextBundleEnum.POLISILOGISMO.toString())),
                javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)));

//        textArea.setColumns(20);
//        textArea.setRows(5);
        textArea.setEditable(false);
        jScrollPane1.setViewportView(textArea);

        analisisPanel.setBorder(javax.swing.BorderFactory.createCompoundBorder(
                javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                        new java.awt.Color(204, 204, 204), 1, true),
                        textBundle.getString(TextBundleEnum.ANALISIS_SILOGISMO_ACTUAL.toString())),
                javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)));

//        analisisTextArea.setColumns(20);
//        analisisTextArea.setRows(5);
        analisisTextArea.setEditable(false);
        jScrollPane2.setViewportView(analisisTextArea);

        sizeTextFields();
        sizeTextAreas();
        layoutComponents();
    }

    /**
     * Realiza el layout de los componentes;
     */
    private void layoutComponents() {
        setBackground(Color.WHITE);
        setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        int x = 0;
        int y = 0;
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        jPanel1.setLayout(new GridBagLayout());

        /*      PREMISAS     */
        jPanel1.add(premisaMayorLabel, gridBagConstraints);

        gridBagConstraints.gridx = (x += 7);
        jPanel1.add(convertirEnLabel, gridBagConstraints);

        gridBagConstraints.gridx = (x -= 7);
        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridwidth = 5;
        jPanel1.add(premisaMayorField, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = (x += 6);

        JButton premisaMayorSugestion = new JButton(
                new FirstPremisaSuggestionAction(PremisaTypeEnum.SECOND, PremisaTypeEnum.CONCLUSION, premisaMayorField));
        premisaMayorSugestion.setToolTipText(textBundle.getString(TextBundleEnum.SUGERENCIA.toString()) + "...");

        jPanel1.add(premisaMayorSugestion, gridBagConstraints);

        gridBagConstraints.gridx = (x += 1);
        jPanel1.add(combo1, gridBagConstraints);

        gridBagConstraints.gridx = (x -= 7);
        gridBagConstraints.gridy = ++y;
        jPanel1.add(premisaMenorLabel, gridBagConstraints);

        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.gridy = ++y;
        jPanel1.add(premisaMenorField, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = (x += 6);
        JButton premisaMenorSugestion = new JButton(
                new FirstPremisaSuggestionAction(PremisaTypeEnum.FIRST, PremisaTypeEnum.CONCLUSION, premisaMenorField));
        premisaMayorSugestion.setToolTipText(textBundle.getString(TextBundleEnum.SUGERENCIA.toString()) + "...");
        jPanel1.add(premisaMenorSugestion, gridBagConstraints);

        gridBagConstraints.gridx = (x += 1);
        jPanel1.add(combo2, gridBagConstraints);

        gridBagConstraints.gridwidth = 7;
        gridBagConstraints.gridx = (x -= 7);
        gridBagConstraints.gridy = ++y;
        jSeparator1.setPreferredSize(new Dimension(550, 2));
        jPanel1.add(jSeparator1, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridy = ++y;
        jPanel1.add(conclusionLabel, gridBagConstraints);

        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridwidth = 5;
        jPanel1.add(conclusionField, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = (x += 6);
        JButton conclusionSugestion = new JButton(
                new FirstPremisaSuggestionAction(PremisaTypeEnum.FIRST, PremisaTypeEnum.SECOND, conclusionField));
        premisaMayorSugestion.setToolTipText(textBundle.getString(TextBundleEnum.SUGERENCIA.toString()) + "...");
        jPanel1.add(conclusionSugestion, gridBagConstraints);

        gridBagConstraints.gridx = (x += 1);
        jPanel1.add(combo3, gridBagConstraints);

        jPanel1.setMinimumSize(new Dimension(655, 250));
        jPanel1.setPreferredSize(new Dimension(655, 250));
        x = 0;
        y = 0;
        /*      END OF PREMISAS     */

        /*      BUTTONS     */
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        add(jPanel1, gridBagConstraints);

        JPanel buttonsPanel = new JPanel(new GridBagLayout());
        buttonsPanel.setBackground(Color.WHITE);
        gridBagConstraints.gridx = x;
        gridBagConstraints.gridy = y;
        gridBagConstraints.insets = new Insets(5, 5, 5, 82);
        gridBagConstraints.gridwidth = 1;
        buttonsPanel.add(ascendenteButton, gridBagConstraints);

        gridBagConstraints.gridx = ++x;
        buttonsPanel.add(descendenteButton, gridBagConstraints);

        gridBagConstraints.gridx = ++x;
        buttonsPanel.add(completeButton, gridBagConstraints);

        gridBagConstraints.gridx = ++x;
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        buttonsPanel.add(resetButton, gridBagConstraints);


        buttonsPanel.setMinimumSize(new Dimension(655, 30));
        buttonsPanel.setPreferredSize(new Dimension(655, 30));
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        add(buttonsPanel, gridBagConstraints);

        x = 0;
        y = 0;

        /*      END OF BUTTONS     */
        analisisPanel.setMinimumSize(new Dimension(655, 200));
        analisisPanel.setPreferredSize(new Dimension(655, 200));
        analisisPanel.add(jScrollPane2);
        analisisPanel.add(completeLabel);
        analisisPanel.add(validLabel);

        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        add(analisisPanel, gridBagConstraints);

        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;

        bottomPanel.setBackground(Color.WHITE);
        bottomPanel.setMinimumSize(new Dimension(655, 150));
        bottomPanel.setPreferredSize(new Dimension(655, 150));
        bottomPanel.add(jScrollPane1);
        add(bottomPanel, gridBagConstraints);

        analize();
    }

    /**
     * Establece el tamano de los textFields;
     */
    private void sizeTextFields() {
        premisaMayorField.setColumns(45);
        premisaMayorField.setMinimumSize(new Dimension(600, 20));
        premisaMenorField.setColumns(45);
        premisaMenorField.setMinimumSize(new Dimension(600, 20));
        conclusionField.setColumns(45);
        conclusionField.setMinimumSize(new Dimension(600, 20));
    }

    /**
     * Establece el tamano de los textAreas;
     */
    private void sizeTextAreas() {
        jScrollPane1.setPreferredSize(new Dimension(640, 115));
//        textArea.setColumns(58);
//        textArea.setEditable(false);
//        textArea.setRows(7);
//        textArea.setPreferredSize(new Dimension(550,110));

//        jScrollPane2.setPreferredSize(new Dimension(640,167));
        jScrollPane2.setPreferredSize(new Dimension(422, 167));
    }

    /**
     * Resetea el silogismo actual;
     */
    private void reset() {
        premisaMayorField.setText("");
        premisaMenorField.setText("");
        conclusionField.setText("");
        sylogism = new Sylogism();
    }

    /**
     * Resetea el polisilogismo;
     */
    private void resetAll() {
        reset();
        premisaMayorField.setEditable(true);
        conclusionField.setEditable(true);
        polisilogismo = new Polisilogismo();
        analize();
    }

    /**
     * Convierte la premisa de acuerdo a la extension seleccionada;
     *
     * @param premisaTypeEnum;
     * @param selectedIndex;
     */
    public void comboConvertOrComplete(PremisaTypeEnum premisaTypeEnum, int selectedIndex) {
        if (sylogism.getPremisa(premisaTypeEnum) != null) {
            switch (selectedIndex) {
                case 0://A
                    sylogism.getPremisa(premisaTypeEnum).convert(ExtensionEnum.A);
                    break;
                case 1://E
                    sylogism.getPremisa(premisaTypeEnum).convert(ExtensionEnum.E);
                    break;
                case 2://I
                    sylogism.getPremisa(premisaTypeEnum).convert(ExtensionEnum.I);
                    break;
                case 3://O
                    sylogism.getPremisa(premisaTypeEnum).convert(ExtensionEnum.O);
                    break;
                case 4://todo getRandomPremisa=>permite completar una premisa en particular
                    break;
            }
            switch (premisaTypeEnum) {
                case FIRST:
                    premisaMayorField.setText(sylogism.getPremisa(premisaTypeEnum).toString());
                    break;
                case SECOND:
                    premisaMenorField.setText(sylogism.getPremisa(premisaTypeEnum).toString());
                    break;
                case CONCLUSION:
                    conclusionField.setText(sylogism.getPremisa(premisaTypeEnum).toString());
                    break;
            }
        }
//        setPropertiesOrSuggestions();
    }

    /**
     * Permite setear el polisilogismo;
     *
     * @param polisilogismo;
     */
    public void setPolisilogismo(Polisilogismo polisilogismo) {
        sylogism = new Sylogism();
        this.polisilogismo = polisilogismo;
        analize();
    }

    public void setSylogism(Sylogism sylogism) {
        this.sylogism = sylogism;
        if (sylogism.getPremisa(PremisaTypeEnum.FIRST) != null) {
            premisaMayorField.setText(sylogism.getPremisa(PremisaTypeEnum.FIRST).toString());
        }
        if (sylogism.getPremisa(PremisaTypeEnum.SECOND) != null) {
            premisaMenorField.setText(sylogism.getPremisa(PremisaTypeEnum.SECOND).toString());
        }
        if (sylogism.getPremisa(PremisaTypeEnum.CONCLUSION) != null) {
            conclusionField.setText(sylogism.getPremisa(PremisaTypeEnum.CONCLUSION).toString());
        }
        analize();
    }

    /**
     * Realiza el analisis del silogismo actual;
     */
    private void analize() {
        manageCompleteButton();

        System.out.println("" + polisilogismo.toString());
        textArea.setText(polisilogismo.toString());
        StringBuffer textAreaText = new StringBuffer();
        if (!sylogism.isComplete()) {
            textAreaText.append(textBundle.getString(TextBundleEnum.SUGERENCIAS.toString()) + ":\n");
            textAreaText.append(sylogism.getPremisaSuggestion());
            textAreaText.append("\n-------------------------------------------------------------------\n");
        }
        setValidCompleteImages();
        textAreaText.append(textBundle.getString(TextBundleEnum.TERMINOS.toString()) + ":\n");
        textAreaText.append(getTerminos());//en su lugar se deberia setear los terminos en los labels
        textAreaText.append("\n-------------------------------------------------------------------\n");
        textAreaText.append(textBundle.getString(TextBundleEnum.ANALISIS.toString()) + ":\n");
        textAreaText.append(sylogism.analize());
        analisisTextArea.setText(textAreaText.toString());
        analisisTextArea.setCaretPosition(0);
    }

    /**
     * Administra los textFields de acuerdo a un polisilogismo ascendente;
     */
    private void setAscendenteTextField() {
        reset();
        conclusionField.setText(polisilogismo.getFirstSylogism().getPremisa(PremisaTypeEnum.FIRST).toString());
        sylogism.setPremisa(polisilogismo.getFirstSylogism().getPremisa(PremisaTypeEnum.FIRST),
                PremisaTypeEnum.CONCLUSION);
        conclusionField.setEditable(false);
        combo3.setEnabled(false);
        premisaMayorField.setEditable(true);
        combo1.setEnabled(true);
        premisaMenorField.setEditable(true);
        analize();
    }

    /**
     * Administra los textFields de acuerdo a un polisilogismo descendente;
     */
    private void setDescendenteTextField() {
        reset();
        premisaMayorField.setText(polisilogismo.getLastSylogism().getPremisa(PremisaTypeEnum.CONCLUSION).toString());
        sylogism.setPremisa(polisilogismo.getFirstSylogism().getPremisa(PremisaTypeEnum.CONCLUSION),
                PremisaTypeEnum.FIRST);
        conclusionField.setEditable(true);
        premisaMayorField.setEditable(false);
        combo1.setEnabled(false);
        premisaMenorField.setEditable(true);
        combo3.setEnabled(true);
        analize();
    }

    /**
     * Permite obtener una representacion de los terminos del silogismo;
     *
     * @return String;
     */
    private String getTerminos() {
        return "\t" + textBundle.getString(TextBundleEnum.TERMINO_MAYOR.toString()) + ": " + sylogism.getTerminoMayor() + "\n" +
                "\t" + textBundle.getString(TextBundleEnum.TERMINO_MEDIO.toString()) + ": " + sylogism.getTerminoMedio() + "\n" +
                "\t" + textBundle.getString(TextBundleEnum.TERMINO_MENOR.toString()) + ": " + sylogism.getTerminoMenor();
    }

    /**
     * Habilita o desabilita el boton "Completar" de acuerdo posea suficiente informacion como para
     * completar la premisa;
     */
    private void manageCompleteButton() {
        int i = 0;
        if (!sylogism.getTerminoMayor().equals("")) {
            i++;
        }
        if (!sylogism.getTerminoMedio().equals("")) {
            i++;
        }
        if (!sylogism.getTerminoMenor().equals("")) {
            i++;
        }
        if (i >= 2) {
            completeButton.setEnabled(true);
        } else {
            completeButton.setEnabled(false);
        }
    }

    /**
     * Setea las imagenes "Valido" y "Completo" en verde o rojo si es valido/completo o
     * invalido/incompleto respectivamente;
     */
    private void setValidCompleteImages() {
        if (!sylogism.isComplete()) {
            completeLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.INCOMPLETO)));
        } else {
            completeLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.COMPLETO)));
        }
        if (sylogism.validate()) {
            validLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.VALIDO)));
        } else {
            validLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.INVALIDO)));
        }
    }

    protected void setSaveActionListener() {
        class SavePoliSylogismAction extends AbstractAction {
            public SavePoliSylogismAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.SAVE_AS)));
            }

            public void actionPerformed(ActionEvent e) {
                if (!polisilogismo.initialSylogismWasSet()) {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                            textBundle.getString(TextBundleEnum.WARNING_NO_GUARDAR_POLISILOGISMO_VACIO.toString()) + "!",
                            "", JOptionPane.WARNING_MESSAGE);
                } else {
                    String path = "";
                    int state = fileChooser.showSaveDialog(JOptionPane.getRootFrame());
                    if (state == JFileChooser.APPROVE_OPTION) {
                        path = fileChooser.getSelectedFile().getPath();
                        Utiles.saveTextFileAlertingOverwrite(path, ".xml", polisilogismo.toXML());
                    }
                }
            }
        }
        saveButton.setAction(new SavePoliSylogismAction());
    }

    protected void setSaveButtonEnabled() {
        saveButton.setEnabled(true);
    }

    protected void setOpenActionListener() {
        class OpenPolisylogismAction extends AbstractAction {
            public OpenPolisylogismAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.OPEN)));
            }

            public void actionPerformed(ActionEvent e) {
                String path = "";
                int state = fileChooser.showOpenDialog(JOptionPane.getRootFrame());
                if (state == JFileChooser.APPROVE_OPTION) {
                    path = fileChooser.getSelectedFile().getPath();
                    try {
                        RandomAccessFile randomAccessFile = new RandomAccessFile(path, "r");
                        String xml = randomAccessFile.readUTF();
                        if (xml.startsWith(Polisilogismo.openTag())) {
                            setPolisilogismo(Polisilogismo.parseXML(xml));
                            setDescendenteTextField();
                        } else {
                            if (xml.startsWith(Sylogism.openTag())) {
                                polisilogismo = new Polisilogismo();
                                polisilogismo.setInitialSylogism(Sylogism.parseXML(xml));
                                setPolisilogismo(polisilogismo);
                                setDescendenteTextField();
                            } else {
                                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                                        textBundle.getString(TextBundleEnum.WARNING_ARCHIVO_INCORRECTO_POLISILOGISMO.toString()));
                            }
                        }
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        openButton.setAction(new OpenPolisylogismAction());
    }

    protected void setOpenButtonEnabled() {
        openButton.setEnabled(true);
    }

    protected void setFocusOnComponent() {
        //remains unimplemented...
    }

    class FirstPremisaSuggestionAction extends AbstractAction {
        private PremisaTypeEnum first;
        private PremisaTypeEnum second;
        private JTextField newPremisaField;

        public FirstPremisaSuggestionAction(PremisaTypeEnum first, PremisaTypeEnum second, JTextField newPremisaField) {
            super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.LIGHT_BULB)));
            this.first = first;
            this.second = second;
            this.newPremisaField = newPremisaField;
        }

        public void actionPerformed(ActionEvent e) {
            StringBuffer terms = new StringBuffer();
            Premisa firstPremisa = sylogism.getPremisa(first);
            Premisa secondPremisa = sylogism.getPremisa(second);
            if (firstPremisa != null) {
                terms.append(firstPremisa.getFirstTerm() + "," + firstPremisa.getSecondTerm());
            }
            if (secondPremisa != null) {
                if (terms != null) {
                    terms.append(",");
                }
                terms.append(secondPremisa.getFirstTerm() + "," + secondPremisa.getSecondTerm());
            }
            if (terms != null) {
                Premisa newPremisa = Premisa.getRandomPremisa();
                newPremisaField.setText(newPremisa.toString());
            } else {
                //todo complete context sensitive Premisa...
            }
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("PolisilogismoPanel");
        frame.setContentPane(new PolisilogismoPanel(new JButton(), new JButton(), new JFileChooser()));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}